package com.easylinkin.linkappapi.device.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.DevicePosition;
import com.easylinkin.linkappapi.device.entity.vo.DevicePositionVo;
import com.easylinkin.linkappapi.device.service.DevicePositionService;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * DevicePosition表控制层
 *
 * @author CodeGenerator
 * @date 2022/06/16
 */
@RestController
@RequestMapping("appDevicePosition")
public class DevicePositionController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private DevicePositionService appDevicePositionService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<DevicePositionVo> requestModel, @RequestParam(value = "usePlace", required = false) String usePlace) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<DevicePositionVo> record = appDevicePositionService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams(),usePlace);
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.appDevicePositionService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param devicePositions 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增")
    @CommonOperateLogAnnotate(module = LogModule.DASHBOARD_SETTING, desc = "数字工地设置")
    public RestMessage saveMulti(@RequestBody List<DevicePosition> devicePositions) {
        return RestBuilders.successBuilder().success((this.appDevicePositionService.saveMulti(devicePositions))).build();
    }

//    /**
//     * 修改数据
//     *
//     * @param devicePositions 实体对象
//     * @return 修改结果
//     */
//    @PutMapping
//    @ApiOperation("修改多条")
//    public RestMessage updateMulti(@RequestBody List<DevicePosition> devicePositions) {
//        return RestBuilders.successBuilder().success(this.appDevicePositionService.updateMulti(devicePositions)).build();
//    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        return RestBuilders.successBuilder().success(this.appDevicePositionService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody DevicePositionVo devicePositionVo, HttpServletRequest request, HttpServletResponse response) {
        appDevicePositionService.export(devicePositionVo, request, response);
    }


    @PostMapping("uploadImage")
    @ApiOperation("上传图片")
    public RestMessage uploadImage(@RequestParam("smartConstructionSiteImageUrl") String smartConstructionSiteImageUrl) {
        appDevicePositionService.uploadImage(smartConstructionSiteImageUrl);
        return RestBuilders.successBuilder().data(smartConstructionSiteImageUrl).build();

    }


    @PostMapping("getImage")
    @ApiOperation("上传图片")
    public RestMessage getImage(@RequestParam("smartConstructionSiteImageUrl") String smartConstructionSiteImageUrl) {
        String r = appDevicePositionService.getImage(smartConstructionSiteImageUrl);
        return RestBuilders.successBuilder().data(r).build();
    }
}

